home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / math / ast53src.zip / PLACALC.C < prev    next >
C/C++ Source or Header  |  1996-09-29  |  44KB  |  1,199 lines

  1. /*
  2. ** Astrolog (Version 5.30) File: placalc.c
  3. **
  4. ** IMPORTANT NOTICE: The graphics database and chart display routines
  5. ** used in this program are Copyright (C) 1991-1996 by Walter D. Pullen
  6. ** (Astara@msn.com, http://www.magitech.com/~cruiser1/astrolog.htm).
  7. ** Permission is granted to freely use and distribute these routines
  8. ** provided one doesn't sell, restrict, or profit from them in any way.
  9. ** Modification is allowed provided these notices remain with any
  10. ** altered or edited versions of the program.
  11. **
  12. ** The main planetary calculation routines used in this program have
  13. ** been Copyrighted and the core of this program is basically a
  14. ** conversion to C of the routines created by James Neely as listed in
  15. ** Michael Erlewine's 'Manual of Computer Programming for Astrologers',
  16. ** available from Matrix Software. The copyright gives us permission to
  17. ** use the routines for personal use but not to sell them or profit from
  18. ** them in any way.
  19. **
  20. ** The PostScript code within the core graphics routines are programmed
  21. ** and Copyright (C) 1992-1993 by Brian D. Willoughby
  22. ** (brianw@sounds.wa.com). Conditions are identical to those above.
  23. **
  24. ** The extended accurate ephemeris databases and formulas are from the
  25. ** calculation routines in the program "Placalc" and are programmed and
  26. ** Copyright (C) 1989,1991,1993 by Astrodienst AG and Alois Treindl
  27. ** (alois@azur.ch). The use of that source code is subject to
  28. ** regulations made by Astrodienst Zurich, and the code is not in the
  29. ** public domain. This copyright notice must not be changed or removed
  30. ** by any user of this program.
  31. **
  32. ** Initial programming 8/28,30, 9/10,13,16,20,23, 10/3,6,7, 11/7,10,21/1991.
  33. ** X Window graphics initially programmed 10/23-29/1991.
  34. ** PostScript graphics initially programmed 11/29-30/1992.
  35. ** Last code change made 9/22/1996.
  36. */
  37.  
  38. #include "placalc.h"
  39.  
  40.  
  41. #ifdef PLACALC
  42. /*
  43. ** ---------------------------------------------------------------
  44. ** | Copyright Astrodienst AG and Alois Treindl, 1989,1991,1993  |
  45. ** | The use of this source code is subject to regulations made  |
  46. ** | by Astrodienst Zurich. The code is NOT in the public domain.|
  47. ** |                                                             |
  48. ** | This copyright notice must not be changed or removed        |
  49. ** | by any user of this program.                                |
  50. ** ---------------------------------------------------------------
  51. **
  52. ** Important changes:
  53. ** 11-jun-93 revision 1.12: fixed error which affected Mercury between -2100
  54. ** and -3100 (it jumped wildly).
  55. */
  56.  
  57. /* function calc():
  58. ** This is the main routine for computing a planets position.
  59. ** The function has several modes, which are controlled by bits in
  60. ** the parameter 'flag'. The normal mode (flag == 0) computes
  61. ** a planets apparent geocentric position in ecliptic coordinates relative to
  62. ** the true equinox of date, without speed
  63. **
  64. ** Explanation of the arguments: see the functions header.
  65. **
  66. ** Returns OK or ERR (if some planet out of time range). OK and ERR are
  67. ** defined in ourdef.h and must not be confused with TRUE and FALSE.
  68. ** OK and ERR are of type int, not of type BOOLEAN.
  69. **
  70. ** Bits used in flag:
  71. ** CALC_BIT_HELIO     0 = geocentric, 1 = heliocentric
  72. ** CALC_BIT_NOAPP       0 = apparent positions, 1 = true positions
  73. ** CALC_BIT_NONUT     0 = do nutation (true equinox of date)
  74. ** 1 = don't do nutation (mean equinox of date).
  75. **
  76. ** CALC_BIT_SPEED     0 = don't calc speed,
  77. ** 1 = calc speed, takes quite long for moon
  78. ** (is observed only for moon, with other
  79. ** planets speed is cheap)
  80. **
  81. ** Side effects and local memory:
  82. ** For doing heliocentric positions the fucntion must know the
  83. ** earth's position for the desired time t. It remembers the earth
  84. ** position so it does not have to recompute it each time a planet
  85. ** position is wanted for the same time t.
  86. ** It calls helup(t), which leaves as a side effect the global
  87. ** variables meanekl, ekl and nut for the time t.
  88. **
  89. ** Functions called by calc():
  90. ** helup(t)
  91. ** hel(t)
  92. ** moon(t)
  93. ** togeo()
  94. **
  95. ** Time range:
  96. ** The function can be used savely in the time range 5000 BC to
  97. ** 3000 AD. The stored ephemeris is available only for this time
  98. ** range, so Jupiter ... Pluto cannot be computed outside. The
  99. ** function will return results for the other planets also outside
  100. ** of this time range, but they become meaningless pretty soon
  101. ** before 5000 BC, because Newcombs time series expansions for the
  102. ** elements will not work anymore.
  103. **
  104. ** pointers to the return variables:
  105. ** alng = ecliptic longitude in degrees
  106. ** arad = radius vector in AU (astronomic units)
  107. ** alat = ecliptic latitude in degrees
  108. ** alngspeed = speed of planet in degrees per day
  109. **
  110. ** !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  111. ** The precision of the speed is quite limited.
  112. ** !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  113. **
  114. ** For Sun, Mercury, Venus and Mars we take only the speed from
  115. ** the undisturbed Kepler orbit. For the Moon there is no
  116. ** reasonable undisturbed orbit and we derive the speed from
  117. ** its position at t + dt and t - dt. We need these
  118. ** moon positions anyway for the true node calculation.
  119. ** For the outer planets and Chiron we derive the precise
  120. ** speed from the stored ephemeris by high order inter-
  121. ** polation; the precision is limited for the geocentric
  122. ** case due to the limited precision of the earth's/sun's speed.
  123. ** Applications who need precise speeds should
  124. ** get them by calling calc() with slightly different times.
  125. **
  126. ** Comment 7 May 1991 by Alois Treindl:
  127. ** Center of Earth versus Barycenter Earth-Moon:
  128. ** Brown's theory of the moon gives the moon's coordinates relative
  129. ** to the center of the earth. Newcomb's theory of the Sun gives the
  130. ** coordinates of the earth's center relative to the center of the Sun.
  131. ** This is what we need.
  132. **
  133. ** How about the Mean Lunar Node?
  134. ** The orbital elements of the Sun in Newcomb's theory are given
  135. ** relative to the barycenter Earth-Moon; the reduction to geocentric
  136. ** is only applied after doing the Kepler ellipse calculation.
  137. ** Are the Lunar elements also relative to the barycenter??
  138. ** If yes:
  139. ** When we use the moon's mean node out of the elements, it is still
  140. ** as seen from the barycenter. Because the node is close to the
  141. ** earth, we would have to apply a considerable correction, which is of
  142. ** the order of 4000/384000 km or 35' (minutes of arc).
  143. ** Nobody has ever applied such a correction to the mean node.
  144. **
  145. ** And the True Node?
  146. ** When we calculate the osculating orbital elements of the Moon (true node),
  147. ** are they relative to the barycenter or to the Earth's center?
  148. ** Our derivation of true node from the actual Moon positions considers
  149. ** the earth's center as the focal point of the osculating lunar ellipse.
  150. ** A more correct approach would first reduce the lunar position from
  151. ** geocentric to barycentric, then compute the orbital elements from
  152. ** the reduced positions, and then reduce the desired items
  153. ** (node, apogaeum, 'dark moon') to geocentric positions.
  154. ** No known astrological ephemeris has ever used such a correction, which is
  155. ** of the same order of magnitude as the correction to the meannode above.
  156. ** When the moon is going through the ecliptic, the geocenter, barycenter
  157. ** moon (and the node identical to the moon itself) line up; this is why
  158. ** the error does not show up in normal considerations.
  159. */
  160.  
  161. int calc(planet, jd_ad, flag, alng, arad, alat, alngspeed)
  162. int planet;  /* planet index as defined in placalc.h,
  163. SUN = 0, MOON = 1 etc.
  164. planet == -1 calc calculates only nut and ecl */
  165. REAL8 jd_ad;  /* relative Astrodienst Juldate, ephemeris time.
  166. Astrodienst Juldate is relative 31 Dec 1949, noon. */
  167. int flag;  /* See definition of flag bits above */
  168. REAL8 *alng;
  169. REAL8 *arad;
  170. REAL8 *alat;
  171. REAL8 *alngspeed;
  172. {
  173.   struct rememberdat  /* time for which the datas are calculated */
  174.     {REAL8 calculation_time, lng, rad, zet, lngspeed, radspeed, zetspeed;};
  175.   static struct rememberdat earthrem =
  176.     {HUGE8, HUGE8, HUGE8, HUGE8, HUGE8, HUGE8, HUGE8};
  177.   static struct rememberdat moonrem  =
  178.     {HUGE8, HUGE8, HUGE8, HUGE8, HUGE8, HUGE8, HUGE8};
  179.   REAL8 c, s, x, knn, knv;
  180.   REAL8 rp, zp; /* needed to call hel! */
  181.   REAL8 *azet = alat;
  182.   BOOLEAN calc_geo, calc_helio, calc_apparent, calc_speed,
  183.   calc_nut;
  184.  
  185.   /* helup checks whether it was already called with same time */
  186.   helup (jd_ad);
  187.   /* we could return now if we only wanted to compute ecl and nut */
  188.  
  189.   calc_helio =  flag & CALC_BIT_HELIO;
  190.   calc_geo = ! calc_helio;
  191.   calc_apparent = ! (flag & CALC_BIT_NOAPP);
  192.   calc_nut = ! (flag & CALC_BIT_NONUT);
  193.   calc_speed = flag & CALC_BIT_SPEED;
  194.   /*
  195.   ** it is necessary to compute EARTH in the following cases:
  196.   ** heliocentric MOON or EARTH
  197.   ** geocentric any planet except MOON or nodes or LILITH
  198.   */
  199.   if (calc_helio && (planet == MOON || planet == EARTH)
  200.     || calc_geo && planet != MOON
  201.     && planet != MEAN_NODE
  202.     && planet != TRUE_NODE
  203.     && planet != LILITH) {
  204.     if (earthrem.calculation_time != jd_ad) {
  205.       hel (EARTH, jd_ad, alng, arad, azet, alngspeed, &rp, &zp);
  206.       /* store earthdata for geocentric calculation: */
  207.       earthrem.lng = *alng;
  208.       earthrem.rad = *arad;
  209.       earthrem.zet = *azet;
  210.       earthrem.lngspeed = *alngspeed;
  211.       earthrem.radspeed = rp;
  212.       earthrem.zetspeed = zp;
  213.       earthrem.calculation_time = jd_ad;
  214.     }
  215.   }
  216.   switch(planet) {
  217.  
  218.   case EARTH: /* has been already computed */
  219.     *alng = earthrem.lng;
  220.     *arad = earthrem.rad;
  221.     *azet = earthrem.zet;
  222.     *alngspeed = earthrem.lngspeed;
  223.     rp = earthrem.radspeed;
  224.     zp = earthrem.zetspeed;
  225.     if (calc_geo) { /* SUN seen from earth */
  226.       *alng = smod8360(*alng + 180.0);
  227.       *azet = - *azet;
  228.     }
  229.     if (calc_apparent)
  230.       *alng = *alng - 0.0057683 * (*arad) * (*alngspeed);
  231.     break;
  232.  
  233.   case MOON:
  234.     moon(alng, arad, azet);
  235.     moonrem.lng = *alng;  /* moonrem will be used for TRUE_NODE */
  236.     moonrem.rad = *arad;
  237.     moonrem.zet = *azet;
  238.     *alngspeed = 12;
  239.     moonrem.calculation_time = jd_ad;
  240.     if (calc_helio || calc_speed) {/* get a second moon position */
  241.       REAL8 lng2, rad2, zet2;
  242.       helup(jd_ad + MOON_SPEED_INTERVAL);
  243.       moon(&lng2, &rad2, &zet2);
  244.       helup(jd_ad);
  245.       if (calc_helio) { /* moon as seen from sun */
  246.         togeo(earthrem.lng, -earthrem.rad, moonrem.lng, moonrem.rad,
  247.         moonrem.zet, alng, arad);
  248.         togeo(earthrem.lng + MOON_SPEED_INTERVAL * earthrem.lngspeed,
  249.         -(earthrem.rad + MOON_SPEED_INTERVAL * earthrem.radspeed),
  250.         lng2, rad2, zet2, &lng2, &rad2);
  251.       }
  252.       *alngspeed =  diff8360(lng2, *alng) / MOON_SPEED_INTERVAL;
  253.       /* rp = (rad2 - *arad) / MOON_SPEED_INTERVAL;       */
  254.       /* zp = (zet2 - moonrem.zet) / MOON_SPEED_INTERVAL; */
  255.     }
  256.     *alat = RADTODEG * ASIN8(*azet / *arad);
  257.     /*
  258.     ** light time correction, not applied for moon or nodes;
  259.     ** moon would have only term of ca. 0.02", see Expl.Sup.1961 p.109
  260.     */
  261.     break;
  262.  
  263.   case MERCURY:
  264.   case VENUS:
  265.   case MARS:
  266.   case JUPITER:
  267.   case SATURN:
  268.   case URANUS:
  269.   case NEPTUNE:
  270.   case PLUTO:
  271.   case CHIRON:
  272.   case CERES:
  273.   case PALLAS:
  274.   case JUNO:
  275.   case VESTA:
  276.     if (hel(planet, jd_ad, alng, arad, azet, alngspeed, &rp, &zp) != OK)
  277.       return ERR; /* outer planets can fail if out of ephemeris range */
  278.     if (calc_geo) {       /* geocentric */
  279.       REAL8 lng1, rad1, lng2, rad2;
  280.       togeo(earthrem.lng, earthrem.rad, *alng, *arad, *azet, &lng1, &rad1);
  281.       togeo(earthrem.lng + earthrem.lngspeed,
  282.       earthrem.rad + earthrem.radspeed,
  283.       *alng + *alngspeed, *arad + rp, *azet + zp, &lng2, &rad2);
  284.       *alng = lng1;
  285.       *arad = rad1;
  286.       *alngspeed = diff8360(lng2, lng1);
  287.       /* rp = rad2 - rad1; */
  288.     }
  289.     *alat = RADTODEG * ASIN8(*azet / *arad);
  290.     if (calc_apparent)
  291.       *alng = *alng - 0.0057683 * (*arad) * (*alngspeed);
  292.     break;
  293.  
  294.   case MEAN_NODE:
  295.     *alng = smod8360(el[MOON].kn);
  296.     /*
  297.     * the distance of the node is the 'orbital parameter' p = a (1-e^2);
  298.     * Our current use of the axis a is wrong, but is never used.
  299.     */
  300.     *arad = pd[MOON].axis;
  301.     *alat = 0.0;
  302.     *alngspeed = -0.053;
  303.     break;
  304.  
  305.   case TRUE_NODE: {
  306.     /* see comment 'Note 7 May 1991' above */
  307.     REAL8 ln, rn, zn,
  308.     lv, rv, zv,
  309.     l1, r1, z1,
  310.     xn, yn, xv, yv, r0, x0, y0;
  311.  
  312.     helup(jd_ad + NODE_INTERVAL);
  313.     moon(&ln, &rn, &zn);
  314.     helup(jd_ad - NODE_INTERVAL);
  315.     moon(&lv, &rv, &zv);
  316.     helup(jd_ad);
  317.     if (moonrem.calculation_time != jd_ad)
  318.       moon(&l1, &r1, &z1);
  319.     else {  /* moon is already calculated */
  320.       l1 = moonrem.lng;
  321.       r1 = moonrem.rad;
  322.       z1 = moonrem.zet;
  323.     }
  324.     rn = sqrt(rn * rn - zn * zn);
  325.     rv = sqrt(rv * rv - zv * zv);
  326.     r0 = sqrt(r1 * r1 - z1 * z1);
  327.     xn = rn * COS8(DEGTORAD * ln);
  328.     yn = rn * SIN8(DEGTORAD * ln);
  329.     xv = rv * COS8(DEGTORAD * lv);
  330.     yv = rv * SIN8(DEGTORAD * lv);
  331.     x0 = r0 * COS8(DEGTORAD * l1);
  332.     y0 = r0 * SIN8(DEGTORAD * l1);
  333.     x = test_near_zero(x0 * yn - xn * y0);
  334.     s = (y0 * zn - z1 * yn) / x;
  335.     c = test_near_zero((x0 * zn - z1 * xn) / x);
  336.     knn =  smod8360(RADTODEG * ATAN28(s, c)); /* = ATAN8(s / c) */
  337.     x = test_near_zero(y0 * xv - x0 * yv);
  338.     s = (yv * z1 - zv * y0) / x;
  339.     c = test_near_zero((xv * z1 - zv * x0) / x);
  340.     knv =  smod8360(RADTODEG * ATAN28(s, c));
  341.     *alng = smod8360((knv + knn) / 2);
  342.     /*
  343.     ** the distance of the node is the 'orbital parameter' p = a (1-e^2);
  344.     ** Our current use of the axis a is wrong.
  345.     */
  346.     *arad = pd[MOON].axis;
  347.     *alat = 0.0;
  348.     *alngspeed = diff8360(knn, knv) / NODE_INTERVAL;
  349.     }
  350.     break;
  351.  
  352.   case LILITH: {
  353.     /*
  354.     ** Added 22-Jun-93
  355.     ** Lilith or Dark Moon is the empty focal point of the mean lunar ellipse.
  356.     ** This is 180 degrees from the perihel.
  357.     ** Because the lunar orbit is not in the ecliptic, it must be
  358.     ** projected onto the ecliptic in the same way as the planetary orbits
  359.     ** are (see for example Montenbruck, Grundlagen der Ephemeridenrechnung).
  360.     **
  361.     ** We compute the MEAN Lilith, not the TRUE one which would have to be
  362.     ** derived in a similar way as the true node.
  363.     ** For the radius vector of Lilith we use a simple formula;
  364.     ** to get a precise value, the fact that the focal point of the ellipse
  365.     ** is not at the center of the earth but at the barycenter moon-earth
  366.     ** would have to be accounted for.
  367.     ** For the speed we always return a constant: the T term from the
  368.     ** lunar perihel.
  369.     ** Joelle de Gravelaine publishes in her book "Lilith der schwarze Mond"
  370.     ** (Astrodata, 1990) an ephemeris which gives noon (12.00) positions
  371.     ** but does not project onto the ecliptic.
  372.     ** This creates deviations
  373.     */
  374.     double arg_lat, lon, cosi;
  375.     struct elements *e = &el[MOON];
  376.     arg_lat = degnorm(e->pe - e->kn + 180.0);
  377.     cosi = COSDEG(e->in);
  378.     if (e->in == 0 || ABS8(arg_lat -  90.0) < TANERRLIMIT
  379.       || ABS8(arg_lat - 270.0) < TANERRLIMIT) {
  380.       lon = arg_lat;
  381.     } else {
  382.       lon = ATAN8(TANDEG(arg_lat) * cosi);
  383.       lon = RADTODEG * lon;
  384.       if (arg_lat > 90.0 && arg_lat < 270.0)  lon += 180.0;
  385.     }
  386.     lon = degnorm(lon + e->kn);
  387.     *alng = lon;
  388.     *alngspeed = 0.111404;  /* 6'41.05" per day */
  389.     *arad = 2 * pd[MOON].axis * e->ex;
  390.     /*
  391.     ** To test Gravalaines error, return unprojected long in alat.
  392.     ** the correct latitude would be:
  393.     ** *alat = RADTODEG * ASIN8(SINDEG(arg_lat) * SINDEG(e->in));
  394.     */
  395. #ifdef ASTROLOG
  396.     *alat = RADTODEG * ASIN8(SINDEG(arg_lat) * SINDEG(e->in));
  397. #else
  398.     *alat = degnorm(arg_lat + e->kn); /* unprojected longitude, no nut */
  399. #endif
  400.     }
  401.     break;
  402.  
  403.   default:
  404.     return ERR;
  405.   } /* end switch */
  406.  
  407.   if (calc_nut)
  408.     *alng += nut;
  409.   *alng = smod8360(*alng);  /* normalize to circle */
  410.   return OK;
  411. }
  412.  
  413.  
  414. /* helio to geocentric conversion */
  415.  
  416. void togeo(lngearth, radearth, lng, rad, zet, alnggeo, aradgeo)
  417. REAL8 lngearth;
  418. REAL8 radearth;
  419. REAL8 lng;
  420. REAL8 rad;
  421. REAL8 zet;
  422. REAL8 *alnggeo;
  423. REAL8 *aradgeo;
  424. {
  425.   REAL8 r1, x, y;
  426.  
  427.   r1 = sqrt(rad * rad - zet * zet);
  428.   x = r1 * COS8(DEGTORAD * lng) - radearth * COS8(DEGTORAD * lngearth);
  429.   y = r1 * SIN8(DEGTORAD * lng) - radearth * SIN8(DEGTORAD * lngearth);
  430.   *aradgeo = sqrt(x * x + y * y + zet * zet);
  431.   x = test_near_zero(x);
  432.   *alnggeo = smod8360(RADTODEG * ATAN28(y, x));
  433. }
  434.  
  435.  
  436. /*
  437. ** helup()
  438. ** prepares the orbital elements and the disturbation arguments for the
  439. ** inner planets and the moon. helup(t) is called by hel() and by calc().
  440. ** helup() returns its results in global variables.
  441. ** helup() remembers the t it has been called with before and does
  442. ** not recalculate its results when it is called more than once with
  443. ** the same t.
  444. */
  445.  
  446. void helup(jd_ad)
  447. REAL8 jd_ad;
  448. {
  449.   int i;
  450.   static REAL8 thelup = HUGE8;  /* is initialized only once at load time */
  451.   struct elements *e = el;      /* pointer to el[i] */
  452.   struct elements *ee = el;     /* pointer to el[EARTH] */
  453.   struct eledata  *d = pd;      /* pointer to pd[i] */
  454.   REAL8 td, ti, ti2, tj1, tj2, tj3;
  455.  
  456.   if (thelup == jd_ad)
  457.     return; /* if already calculated then return */
  458.  
  459.   for (i = SUN; i <= MARS; i++, d++, e++) {
  460.     td = jd_ad - d->epoch;
  461.     ti = e->tj = td / 36525.0;  /* julian centuries from epoch */
  462.     ti2 = ti * ti;
  463.     tj1 = ti / 3600.0;  /* used when coefficients are in seconds of arc */
  464.     tj2 = ti * tj1;
  465.     tj3 = ti * tj2;
  466.     e->lg = mod8360(d->lg0 + d->lg1 * td  + d->lg2 * tj2 + d->lg3 * tj3);
  467.     /* also with moon lg1 *td is exact to 10e-8 degrees within 5000 years */
  468.     e->pe = mod8360(d->pe0 + d->pe1 * tj1 + d->pe2 * tj2 + d->pe3 * tj3);
  469.     e->ex = d->ex0 + d->ex1 * ti + d->ex2 * ti2;
  470.     e->kn = mod8360(d->kn0 + d->kn1 * tj1 + d->kn2 * tj2 + d->kn3 * tj3);
  471.     e->in = d->in0 + d->in1 * tj1 + d->in2 * tj2;
  472.     e->ma = smod8360(e->lg - e->pe);
  473.  
  474.     if (i == MOON) {
  475.       /* calculate ekliptic according Newcomb, APAE VI,
  476.       ** and nutation according Exp.Suppl. 1961, identical
  477.       ** with Mark Potttenger elemnut()
  478.       ** all terms >= 0.01" only .
  479.       ** The 1984 IAU Theory of Nutation, as published in
  480.       ** AE 1984 suppl. has not yet been implemented
  481.       ** because it would mean to use other elements of
  482.       ** moon and sun */
  483.  
  484.       REAL8 mnode, mlong2, slong2, mg, sg, d2;
  485.  
  486.       mnode  = DEGTORAD * e->kn;        /* moon's mean node */
  487.       mlong2 = DEGTORAD * 2.0 * e->lg;  /* 2 x moon's mean longitude */
  488.       mg     = DEGTORAD * e->ma;        /* moon's mean anomaly (g1) */
  489.       slong2 = DEGTORAD * 2.0 * ee->lg; /* 2 x sun's mean longitude (L), with
  490.                                         the phase 180 deg earth-sun irrelevant
  491.                                         because 2 x 180 = 360 deg */
  492.       sg     = DEGTORAD * ee->ma; /* sun's mean anomaly = earth's */
  493.       d2     = mlong2 - slong2;   /* 2 x elongation of moon from sun */
  494.       meanekl = ekld[0] + ekld[1] * tj1 + ekld[2] * tj2 + ekld[3] * tj3;
  495.       ekl = meanekl +
  496.         (9.2100 * COS8(mnode)
  497.         - 0.0904 * COS8(2.0 * mnode)
  498.         + 0.0183 * COS8(mlong2 - mnode)
  499.         + 0.0884 * COS8(mlong2)
  500.         + 0.0113 * COS8(mlong2 + mg)
  501.         + 0.5522 * COS8(slong2)
  502.         + 0.0216 * COS8(slong2 + sg)) / 3600.0;
  503.         nut = ((-17.2327 - 0.01737 * ti) * SIN8(mnode)
  504.         + 0.2088 * SIN8(2.0 * mnode)
  505.         + 0.0675 * SIN8(mg)
  506.         - 0.0149 * SIN8(mg - d2)
  507.         - 0.0342 * SIN8(mlong2 - mnode)
  508.         + 0.0114 * SIN8(mlong2 - mg)
  509.         - 0.2037 * SIN8(mlong2)
  510.         - 0.0261 * SIN8(mlong2 + mg)
  511.         + 0.0124 * SIN8(slong2 - mnode)
  512.         + 0.0214 * SIN8(slong2 - sg)
  513.         - 1.2729 * SIN8(slong2)
  514.         - 0.0497 * SIN8(slong2 + sg)
  515.         + 0.1261 * SIN8(sg)) / 3600.0;
  516.     }
  517.   }
  518.  
  519.   /* calculate the arguments sa[] for the disturbation terms */
  520.   ti = (jd_ad - EPOCH1850) / 365.25;  /* julian years from 1850 */
  521.   for (i = 0; i < SDNUM; i++)
  522.     sa [i] = mod8360(_sd [i].sd0 + _sd [i].sd1 * ti);
  523.   /*
  524.   ** sa[2] += 0.3315 * SIN8 (DEGTORAD *(133.9099 + 38.39365 * el[SUN].tj));
  525.   **
  526.   ** correction of jupiter perturbation argument for sun from Pottenger;
  527.   ** creates only .03" and 1e-7 rad, not applied because origin unclear */
  528.   thelup = jd_ad;               /* note the last helup time */
  529. }
  530.  
  531.  
  532. /*
  533. ** hel()
  534. ** Computes the heliocentric positions for all planets except the moon.
  535. ** The outer planets from Jupiter onwards, including Chiron, are
  536. ** actually done by a subsequent call to outer_hel() which takes
  537. ** exactly the same parameters.
  538. ** hel() does true position relative to the mean ecliptic and equinox
  539. ** of date. Nutation is not added and must be done so by the caller.
  540. ** The latitude of the Sun (max. 0.5") is neglected and always returned
  541. ** as zero.
  542. **
  543. ** return: OK or ERR
  544. */
  545.  
  546. int hel(planet, t, al, ar, az, alp, arp, azp)
  547. int planet;   /* planet index as defined by placalc.h */
  548. REAL8 t;      /* relative juliand date, ephemeris time */
  549.               /* Now come 6 pointers to return values. */
  550. REAL8 *al;    /* longitude in degrees */
  551. REAL8 *ar;    /* radius in AU */
  552. REAL8 *az;    /* distance from ecliptic in AU */
  553. REAL8 *alp;   /* speed in longitude, degrees per day */
  554. REAL8 *arp;   /* speed in radius, AU per day */
  555. REAL8 *azp;   /* speed in z, AU per day */
  556. {
  557.   register struct elements *e;
  558.   register struct eledata  *d;
  559.   REAL8 lk = 0.0;
  560.   REAL8 rk = 0.0;
  561.   REAL8 b, h1, sini, sinv, cosi, cosu, cosv, man, truanom, esquare,
  562.     k8, u, up, v, vp;
  563.  
  564.   if (planet >= JUPITER)
  565.     return (outer_hel(planet, t, al, ar, az, alp, arp, azp));
  566.   if (planet < SUN || planet == MOON)
  567.     return ERR;
  568.  
  569.   e = &el[planet];
  570.   d = &pd[planet];
  571.   sini = SIN8(DEGTORAD * e->in);
  572.   cosi = COS8(DEGTORAD * e->in);
  573.   esquare = sqrt((1.0 + e->ex) / (1.0 - e->ex)); /* H6 in old version */
  574.   man = e->ma;
  575.   if (planet == EARTH)  /* some longperiodic terms in mean longitude */
  576.     man += (0.266 * SIN8 (DEGTORAD * (31.8 + 119.0 * e->tj))
  577.       + 6.40 * SIN8(DEGTORAD * (231.19 + 20.2 * e->tj))
  578.       + (1.882-0.016*e->tj) * SIN8(DEGTORAD * (57.24 + 150.27 * e->tj))
  579.       ) / 3600.0;
  580.   if (planet == MARS)  /* some longperiodic terms */
  581.     man += (0.606 * SIN8(DEGTORAD * (212.87 + e->tj * 119.051))
  582.       + 52.490 * SIN8(DEGTORAD * (47.48 + e->tj * 19.771))
  583.       +  0.319 * SIN8(DEGTORAD * (116.88 + e->tj * 773.444))
  584.       +  0.130 * SIN8(DEGTORAD * (74 + e->tj * 163))
  585.       +  0.280 * SIN8(DEGTORAD * (300 + e->tj * 40.8))
  586.       -  (37.05 +13.5 * e->tj)
  587.       ) / 3600.0;
  588.   u = fnu (man, e->ex, 0.0000003); /* error 0.001" returns radians */
  589.   cosu = COS8(u);
  590.   h1 = 1 - e->ex * cosu;
  591.   *ar = d->axis * h1;
  592.   if (ABS8(rPi - u) < TANERRLIMIT)
  593.     truanom = u; /* very close to aphel */
  594.   else
  595.     truanom = 2.0 * ATAN8(esquare * TAN8(u * 0.5)); /* true anomaly, rad*/
  596.   v = smod8360(truanom * RADTODEG + e->pe - e->kn); /* argument of latitude */
  597.   if (sini == 0.0 || ABS8(v -  90.0) < TANERRLIMIT
  598.     || ABS8(v - 270.0) < TANERRLIMIT) {
  599.     *al = v;
  600.   } else {
  601.     *al = RADTODEG * ATAN8(TAN8(v * DEGTORAD) * cosi);
  602.     if (v > 90.0 && v < 270.0)  *al += 180.0;
  603.   }
  604.   *al = smod8360(*al + e->kn);
  605.   sinv = SIN8(v * DEGTORAD);
  606.   cosv = COS8(v * DEGTORAD);
  607.   *az = *ar * sinv * sini;
  608.   b = ASIN8(sinv * sini);     /* latitude in radians */
  609.   k8 = cosv / COS8(b) * sini;
  610.   up = 360.0 / d->period / h1;    /* du/dt degrees/day */
  611.   if (ABS8(rPi - u) < TANERRLIMIT)
  612.     vp = up / esquare;  /* speed at aphel */
  613.   else
  614.     vp = up * esquare * (1 + COS8 (truanom)) / (1 + cosu);
  615.   /* dv/dt degrees/day */
  616.   *arp = d->axis * up * DEGTORAD * SIN8(u) * e->ex;
  617.   /* dr/dt AU/day */
  618.   *azp = *arp * sinv * sini + *ar * vp * DEGTORAD * cosv * sini;  /* dz/dt */
  619.   *alp = vp / cosi * (1 - k8 * k8);
  620.  
  621.   /* now come the disturbations */
  622.  
  623.   switch (planet) {
  624.     REAL8 am, mma, ema, u2;
  625.  
  626.   case EARTH:
  627.     /*
  628.     ** earth has some special moon values and a disturbation series due to the
  629.     ** planets. The moon stuff is due to the fact, that the mean elements
  630.     ** give the coordinates of the earth-moon barycenter. By adding the
  631.     ** corrections we effectively reduce to the center of the earth.
  632.     ** We neglect the correction in latitude, which is about 0.5", because
  633.     ** for astrological purposes we want the Sun to have latitude zero.
  634.     */
  635.     am = DEGTORAD * smod8360(el[MOON].lg - e->lg + 180.0); /* degrees */
  636.     mma = DEGTORAD * el[MOON].ma;
  637.     ema = DEGTORAD * e->ma;
  638.     u2 = 2.0 * DEGTORAD * (e->lg - 180.0 - el[MOON].kn); /* 2u' */
  639.     lk = 6.454 * SIN8(am)
  640.       + 0.013 * SIN8(3.0 * am)
  641.       + 0.177 * SIN8(am + mma)
  642.       - 0.424 * SIN8(am - mma)
  643.       + 0.039 * SIN8(3.0 * am - mma)
  644.       - 0.064 * SIN8(am + ema)
  645.       + 0.172 * SIN8(am - ema)
  646.       - 0.013 * SIN8(am - mma - ema)
  647.       - 0.013 * SIN8(u2);
  648.     rk = 13360 * COS8(am)
  649.       + 30    * COS8(3.0 * am)
  650.       + 370   * COS8(am + mma)
  651.       - 1330  * COS8(am - mma)
  652.       + 80    * COS8(3.0 * am - mma)
  653.       - 140   * COS8(am + ema)
  654.       + 360   * COS8(am - ema)
  655.       - 30    * COS8(am - mma - ema)
  656.       + 30    * COS8(u2);
  657.     /* long periodic term from mars 15g''' - 8g'', Vol 6 p19, p24 */
  658.     lk += 0.202 * SIN8(DEGTORAD * (315.6 + 893.3 * e->tj));
  659.     disturb(earthkor, al, ar, lk, rk, man);
  660.     break;
  661.  
  662.   case MERCURY:  /* only normal disturbation series */
  663.     disturb(mercurykor, al, ar, 0.0, 0.0, man);
  664.     break;
  665.  
  666.   case VENUS:  /* some longperiod terms and normal series */
  667.     lk = (2.761 - 0.22*e->tj) * SIN8(DEGTORAD * (237.24 + 150.27 * e->tj))
  668.     + 0.269 * SIN8(DEGTORAD * (212.2  + 119.05 * e->tj))
  669.     - 0.208 * SIN8(DEGTORAD * (175.8  + 1223.5 * e->tj));
  670.     /* make seconds */
  671.     disturb(venuskor, al, ar, lk, 0.0, man);
  672.     break;
  673.  
  674.   case MARS:  /* only normal disturbation series */
  675.     disturb(marskor, al, ar, 0.0, 0.0, man);
  676.     break;
  677.   }
  678.   return OK;
  679. }
  680.  
  681.  
  682. void disturb(k, al, ar, lk, rk, man)
  683. register struct kor *k;  /* ENDMARK-terminated array of struct kor */
  684. REAL8 *al, /* longitude in degrees, use a pointer to return value */
  685. *ar;       /* radius in AU */
  686. REAL8 lk,  /* longitude correction in SECONDS OF ARC */
  687.            /* function can be called with an lk and rk already
  688.               != 0, but no value is returned */
  689. rk,        /* radius correction in units of 9th place of log r */
  690. man;       /* mean anomaly of planet */
  691. {
  692.   REAL8 arg;
  693.   while (k->j != ENDMARK) {
  694.     arg = k->j * sa[k->k] + k->i * man;
  695.     lk += k->lampl * COS8(DEGTORAD * (k->lphase - arg));
  696.     rk += k->rampl * COS8(DEGTORAD * (k->rphase - arg));
  697.     k++;
  698.   }
  699.   *ar *= EXP10(rk * 1.0E-9);  /* 10^rk */
  700.   *al += lk / 3600.0;
  701. }
  702.  
  703.  
  704. int moon(al, ar, az)  /* return OK or ERR */
  705. REAL8 *al;
  706. REAL8 *ar;
  707. REAL8 *az;
  708. {
  709.   REAL8 a1,a2,a3,a4,a5,a6,a7,a8,a9,c2,c4,arg,b,d,f,dgc,dlm,dpm,dkm,dls;
  710.   REAL8 ca, cb, cd, f_2d, f_4d, g1c,lk,lk1,man,ms,nib,s,sinarg,sinp,sk;
  711.   REAL8 t, tb, t2c, r2rad, i1corr, i2corr, dlid;
  712.   int i;
  713.   struct elements *e;
  714.   struct m45dat   *mp;
  715. #if MOON_TEST_CORR
  716.   struct m5dat    *m5p;
  717. #endif
  718.   e = &el[MOON];
  719.   t = e->tj * 36525;  /* days from epoch 1900 */
  720.  
  721.   /* new format table II, parameters in full rotations of 360 degrees */
  722.   r2rad = 360.0 * DEGTORAD;
  723.   tb  = t * 1e-12;  /* units of 10^12 */
  724.   t2c = t * t * 1e-16;  /* units of 10^16 */
  725.   a1 = SIN8(r2rad * (0.53733431 -  10104982 * tb + 191 * t2c));
  726.   a2 = SIN8(r2rad * (0.71995354 - 147094228 * tb +  43 * t2c));
  727.   c2 = COS8(r2rad * (0.71995354 - 147094228 * tb +  43 * t2c));
  728.   a3 = SIN8(r2rad * (0.14222222 +   1536238 * tb));
  729.   a4 = SIN8(r2rad * (0.48398132 - 147269147 * tb +  43 * t2c));
  730.   c4 = COS8(r2rad * (0.48398132 - 147269147 * tb +  43 * t2c));
  731.   a5 = SIN8(r2rad * (0.52453688 - 147162675 * tb +  43 * t2c));
  732.   a6 = SIN8(r2rad * (0.84536324 -  11459387 * tb));
  733.   a7 = SIN8(r2rad * (0.23363774 +   1232723 * tb + 191 * t2c));
  734.   a8 = SIN8(r2rad * (0.58750000 +   9050118 * tb));
  735.   a9 = SIN8(r2rad * (0.61043085 -  67718733 * tb));
  736.  
  737.   dlm = 0.84 * a3 + 0.31 * a7 + 14.27 * a1 + 7.261  * a2 + 0.282 * a4
  738.     + 0.237 * a6;
  739.   dpm = -2.1  * a3 - 2.076  * a2 - 0.840 * a4 - 0.593 * a6;
  740.   dkm = 0.63 * a3 + 95.96 * a2 + 15.58 * a4 + 1.86 * a5;
  741.   dls = -6.4  * a3 - 0.27 * a8 - 1.89  * a6 + 0.20 * a9;
  742.   dgc = (-4.318 * c2 - 0.698 * c4) / 3600.0 / 360.0;  /* in revolutions */
  743.   dgc = (1.000002708 + 139.978 * dgc);  /* in this form used later */
  744.   man = DEGTORAD * (e->ma + (dlm - dpm) / 3600.0);
  745.   /* man with periodic and secular corr. */
  746.   ms  = DEGTORAD * (el[EARTH].ma + dls / 3600.0);
  747.   f   = DEGTORAD * (e->lg - e->kn + (dlm - dkm) / 3600.0);
  748.   d   = DEGTORAD * (e->lg + 180 - el[EARTH].lg + (dlm - dls) / 3600.0);
  749.  
  750.   lk = lk1 = sk = sinp = nib = g1c = 0;
  751.   i1corr = 1.0 - 6.8320E-8 * t;
  752.   i2corr = dgc * dgc; /* i2 occurs only as -2, 2 */
  753.   for (i = 0, mp = m45; i < NUM_MOON_CORR; i++, mp++) {
  754.     /* arg = mp->i0 * man + mp->i1 * ms + mp->i2 * f + mp->i3 * d; */
  755.     arg = mp->i0 * man;
  756.     arg += mp->i3 * d;
  757.     arg += mp->i2 * f;
  758.     arg += mp->i1 * ms;
  759.     sinarg = SIN8(arg);
  760.     /*
  761.     ** now apply corrections due to changes in constants;
  762.     ** we correct only terms in l' (i1) and F (i2), not in l (i0), because
  763.     ** the latter are < 0.05"
  764.     ** We don't apply corrections  for cos(arg), i.e. for parallax
  765.     */
  766.     if (mp->i1 != 0) {  /* i1 can be -2, -1, 0, 1, 2 */
  767.       sinarg *= i1corr;
  768.       if  (mp->i1 == 2 || mp->i1 == -2)
  769.         sinarg *= i1corr;
  770.     }
  771.     if (mp->i2 != 0)  /* i2 can be -2, 0, 2 */
  772.       sinarg *= i2corr;
  773.     lk += mp->lng * sinarg;
  774.     sk += mp->lat * sinarg;
  775.     sinp += mp->par * COS8 (arg) ;
  776.   }
  777.  
  778. #if MOON_TEST_CORR  /* optionally add more lunar longitudes  */
  779.   for (m5p = m5; m5p->i0 != 99; m5p++) {  /* i0 = 99 is end mark */
  780.     arg = m5p->i0 * man + m5p->i1 * ms + m5p->i2 * f + m5p->i3 * d;
  781.     sinarg = SIN8(arg);
  782.     lk1 += m5p->lng * sinarg;
  783.   }
  784. #endif
  785.  
  786.   /*
  787.   ** now compute some planetary terms in longitude, list i delta;
  788.   ** we take all > 0.5" and neglect secular terms in the arguments. These
  789.   ** produce phase errors > 10 degrees only after 3000 years.
  790.   */
  791.   dlid =  0.822 * SIN8 (r2rad * (0.32480 - 0.0017125594 * t));
  792.   dlid += 0.307 * SIN8 (r2rad * (0.14905 - 0.0034251187 * t));
  793.   dlid += 0.348 * SIN8 (r2rad * (0.68266 - 0.0006873156 * t));
  794.   dlid += 0.662 * SIN8 (r2rad * (0.65162 + 0.0365724168 * t));
  795.   dlid += 0.643 * SIN8 (r2rad * (0.88098 - 0.0025069941 * t));
  796.   dlid += 1.137 * SIN8 (r2rad * (0.85823 + 0.0364487270 * t));
  797.   dlid += 0.436 * SIN8 (r2rad * (0.71892 + 0.0362179180 * t));
  798.   dlid += 0.327 * SIN8 (r2rad * (0.97639 + 0.0001734910 * t));
  799.  
  800.   /* without nutation */
  801.   *al = smod8360(e->lg + (dlm + lk + lk1 + dlid) / 3600.0);
  802.  
  803.   /* solar Terms in latitude Nibeta */
  804.   f_2d = f - 2.0 * d;
  805.   f_4d = f - 4.0 * d;
  806.   nib += -526.069 * SIN8(                   f_2d);
  807.   nib +=   -3.352 * SIN8(                   f_4d);
  808.   nib +=   44.297 * SIN8( man             + f_2d);
  809.   nib +=   -6.000 * SIN8( man             + f_4d);
  810.   nib +=   20.599 * SIN8(-man             + f   );
  811.   nib +=  -30.598 * SIN8(-man             + f_2d);
  812.   nib +=  -24.649 * SIN8(-2*man           + f   );
  813.   nib +=   -2.000 * SIN8(-2*man           + f_2d);
  814.   nib +=  -22.571 * SIN8(          ms     + f_2d);
  815.   nib +=   10.985 * SIN8(         -ms     + f_2d);
  816.  
  817.   /* new gamma1C from 29 Jul 88, all terms > 0.4 " in table III, code 2 */
  818.   g1c += -0.725 * COS8(          d);
  819.   g1c +=  0.601 * COS8(      2 * d);
  820.   g1c +=  0.394 * COS8(      3 * d);
  821.   g1c += -0.445 * COS8(man                   + 4 * d);
  822.   g1c +=  0.455 * COS8(man                   + 1 * d);
  823.   g1c +=  5.679 * COS8(2 * man               - 2 * d);
  824.   g1c += -1.300 * COS8(3 * man                      );
  825.   g1c += -1.302 * COS8(            ms               );
  826.   g1c += -0.416 * COS8(            ms        - 4 * d);
  827.   g1c += -0.740 * COS8(        2 * ms        - 2 * d);
  828.   g1c +=  0.787 * COS8(    man +   ms        + 2 * d);
  829.   g1c +=  0.461 * COS8(    man +   ms               );
  830.   g1c +=  2.056 * COS8(    man +   ms        - 2 * d);
  831.   g1c += -0.471 * COS8(    man +   ms        - 4 * d);
  832.   g1c += -0.443 * COS8(   -man +   ms        + 2 * d);
  833.   g1c +=  0.679 * COS8(   -man +   ms               );
  834.   g1c += -1.540 * COS8(   -man +   ms        - 2 * d);
  835.  
  836.   s =  f + sk / 3600.0 * DEGTORAD;
  837.   ca = 18519.7 + g1c;
  838.   cb = -0.000336992 * ca * dgc * dgc * dgc;
  839.   cd = ca / 18519.7;
  840.   b = (ca * SIN8(s) * dgc  + cb * SIN8(3.0 * s) + cd * nib) / 3600.0;
  841.  
  842.   /* we neglect the planetary terms in latitude, code 4 in table III */
  843.  
  844.   sinp = (sinp + 3422.451);
  845.   /*
  846.   ** Improved lunar ephemeris and APAE until ca. 1970 had here
  847.   ** 3422.54 as constant of moon's sine parallax.
  848.   ** The difference can be applied by direct addition of 0.089" to
  849.   ** our parallax results.
  850.   **
  851.   ** To get the radius in A.U. from the sine parallax,
  852.   ** we use 1964 IAU value 8.794" for solar parallax.
  853.   ** sinp is still in seconds of arc.
  854.   ** To calculate moon parallax in " it would be:
  855.   ** p = sinp (1  + sinp * sinp * 3.917405E-12)
  856.   ** based on the formula p = sinp + 1/6 sinp^3
  857.   ** and taking into account the conversion of " to radians.
  858.   ** The semidiameter of the moon is: (Expl.Suppl. 61, p 109)
  859.   ** s = 0.0796 + 0.272446 * p
  860.   */
  861.  
  862.   *ar = 8.794 / sinp;
  863.   *az = *ar * SIN8(DEGTORAD * b);
  864.   return OK;
  865. }
  866.  
  867.  
  868. /*
  869. ** outer_hel()
  870. ** Computes the position of Jupiter, Saturn, Uranus, Neptune, Pluto and
  871. ** Chiron by reading our stored ephemeris in steps of 80 (!) days and
  872. ** applying a high order interpolation to it. The interpolation errors are
  873. ** less than 0.01" seconds of arc.
  874. ** The stored ephemeris is packed in a special format consisting of
  875. ** 32 bit numbers; it has been created on the Astrodienst Unix system
  876. ** by numerical integration with routines provided originally by Marc
  877. ** Pottenger, USA, which we improved for better long term precision.
  878. ** Because the Unix system uses a different byte order than the MSDOS
  879. ** systems, the bytes must be reordered for MSDOS after reading from
  880. ** the binary files.
  881. **
  882. ** outer_hel() takes the same parameters as hel().
  883. ** It returns the same type of values.
  884. **
  885. ** The access to the ephemeris files is done in the functions chi_file_posit()
  886. ** and lrz_file_posit().
  887. */
  888.  
  889. int outer_hel(planet, jd_ad, al, ar, az, alp, arp, azp)
  890. int planet;
  891. REAL8 jd_ad;  /* jd_ad Astrodienst relative Julian ephemeris time */
  892. REAL8 *al;
  893. REAL8 *ar;
  894. REAL8 *az;
  895. REAL8 *alp;
  896. REAL8 *arp;
  897. REAL8 *azp;
  898. {
  899.   static FILE *outerfp = NULL, *chironfp = NULL, *asterfp = NULL;
  900.   static double last_j0_outer = HUGE8;
  901.   static double last_j0_chiron = HUGE8;
  902.   static double last_j0_aster = HUGE8;
  903.   static long icoord[6][5][3], chicoord[6][3], ascoord[6][4][3];
  904.   REAL8 j0, jd, jfrac;
  905.   REAL8 l[6], r[6], z[6];
  906.   int n, order, p;
  907.  
  908.   if ((planet < JUPITER || planet > PLUTO) && planet != CHIRON &&
  909.     (planet < CERES || planet > VESTA))
  910.     return ERR;
  911.   jd = jd_ad + JUL_OFFSET;
  912.   j0 = RFloor((jd - 0.5) / EPHE_STEP) * EPHE_STEP + 0.5;
  913.   jfrac = (jd - j0) / EPHE_STEP;
  914.   if (planet == CHIRON) {
  915.     if (last_j0_chiron != j0) {
  916.       for (n = 0; n < 6; n++) { /* read 6 days */
  917.         jd = j0 + (n - 2) * EPHE_STEP;
  918.         if (chi_file_posit(jd, &chironfp) != OK)
  919.           return ERR;
  920.         fread(&chicoord[n][0], sizeof(word4), 3, chironfp);
  921.         longreorder((UCHAR *)&chicoord[n][0], 3*4);
  922.       }
  923.       last_j0_chiron = j0;
  924.     }
  925.     for (n = 0; n < 6; n++) {
  926.       l[n] = chicoord[n][0] / DEG2MSEC;
  927.       r[n] = chicoord[n][1] / AU2INT;
  928.       z[n] = chicoord[n][2] / AU2INT;
  929.     }
  930.   } else if (planet >= CERES && planet <= VESTA) {
  931.     if (last_j0_aster != j0) {  /* read all 4 asteroids for 6 steps */
  932.       for (n = 0; n < 6; n++) {
  933.         jd = j0 + (n - 2) * EPHE_STEP;
  934.         if (ast_file_posit(jd, &asterfp) != OK)
  935.           return ERR;
  936.         fread(&ascoord[n][0][0], sizeof(word4), 12, asterfp);
  937.         longreorder((UCHAR *)&ascoord[n][0][0], 12*4);
  938.       }
  939.       last_j0_outer = j0;
  940.     }
  941.     p = planet - CERES;
  942.     for (n = 0; n < 6; n++) {
  943.       l[n] = ascoord[n][p][0] / DEG2MSEC;
  944.       r[n] = ascoord[n][p][1] / AU2INT;
  945.       z[n] = ascoord[n][p][2] / AU2INT;
  946.     }
  947.   } else {  /* an outerplanet */
  948.     if (last_j0_outer != j0) {  /* read all 5 planets for 6 steps */
  949.       for (n = 0; n < 6; n++) {
  950.         jd = j0 + (n - 2) * EPHE_STEP;
  951.         if (lrz_file_posit(jd, &outerfp) != OK)
  952.           return ERR;
  953.         fread(&icoord[n][0][0], sizeof(word4), 15, outerfp);
  954.         longreorder((UCHAR *)&icoord[n][0][0], 15*4);
  955.       }
  956.       last_j0_outer = j0;
  957.     }
  958.     p = planet - JUPITER;
  959.     for (n = 0; n < 6; n++) {
  960.       l[n] = icoord[n][p][0] / DEG2MSEC;
  961.       r[n] = icoord[n][p][1] / AU2INT;
  962.       z[n] = icoord[n][p][2] / AU2INT;
  963.     }
  964.   }
  965.   if (planet > SATURN)
  966.     order = 3;
  967.   else
  968.     order = 5;
  969.   inpolq(2, order, jfrac, l, al, alp);
  970.   *alp /= EPHE_STEP;
  971.   inpolq(2, order, jfrac, r, ar, arp);
  972.   *arp /= EPHE_STEP;
  973.   inpolq(2, order, jfrac, z, az, azp);
  974.   *azp /= EPHE_STEP;
  975.   return OK;
  976. }
  977.  
  978.  
  979. /*
  980. ** quicker Everett interpolation, after Pottenger
  981. ** version 9 Jul 1988 by Alois Treindl
  982. ** return OK or ERR.
  983. */
  984.  
  985. int inpolq(n, o, p, x, axu, adxu)
  986. int n,     /* interpolate between x[n] and x[n-1], at argument n+p */
  987. o;         /* order of interpolation, maximum 5 */
  988. double p,  /* argument , intervall [0..1] */
  989. x[],       /* array of function values, x[n-o]..x[n+o] must exist */
  990. *axu,      /* pointer for storage of result */
  991. *adxu;     /* pointer for storage of dx/dt  */
  992. {
  993.   static double q, q2, q3, q4, q5, p2, p3, p4, p5, u, u0, u1, u2;
  994.   static double lastp = 9999;
  995.   double dm2, dm1, d0, dp1, dp2,
  996.     d2m1, d20, d2p1, d2p2, d30, d3p1, d3p2, d4p1, d4p2;
  997.   double offset = 0.0;
  998.  
  999.   if (lastp != p) {
  1000.     q  = 1.0-p;
  1001.     q2 = q*q;
  1002.     q3 = (q+1.0)*q*(q-1.0)/6.0;       /* q - 1 over 3; u5 */
  1003.     p2 = p*p;
  1004.     p3 = (p+1.0)*p*(p-1.0)/6.0;       /* p - 1 over 3; u8 */
  1005.     u  = (3.0*p2-1.0)/6.0;
  1006.     u0 = (3.0*q2-1.0)/6.0;
  1007.     q4 = q2*q2;                       /* f5 */
  1008.     p4 = p2*p2;                       /* f4 */
  1009.     u1 = (5.0*p4-15.0*p2+4.0)/120.0;  /* u1 */
  1010.     u2 = (5.0*q4-15.0*q2+4.0)/120.0;  /* u2 */
  1011.     q5 = q3*(q+2.0)*(q-2.0)/20.0;     /* q - 2 over 5; u6 */
  1012.     p5 = (p+2.0)*p3*(p-2.0)/20.0;     /* p - 2 over 5; u9 */
  1013.     lastp = p;
  1014.   }
  1015.   dm1 = x[n] - x[n-1];
  1016.   if (dm1 > 180.0)
  1017.     dm1 -= 360.0;
  1018.   if (dm1 < -180.0)
  1019.     dm1 += 360.0;
  1020.   d0 = x[n+1] - x[n];
  1021.   if (d0 > 180.0) {
  1022.     d0 -= 360.0;
  1023.     offset = 360.0;
  1024.   }
  1025.   if (d0 < -180.0) {
  1026.     d0 += 360.0;
  1027.     offset = -360.0;
  1028.   }
  1029.   dp1 = x[n+2] - x[n+1];
  1030.   if (dp1 > 180.0)
  1031.     dp1 -= 360.0;
  1032.   if (dp1 < -180.0)
  1033.     dp1 += 360.0;
  1034.   d20  = d0 - dm1;    /* f8 */
  1035.   d2p1 = dp1 - d0;    /* f9 */
  1036.  
  1037.   /* Everett interpolation 3rd order */
  1038.  
  1039.   *axu = q*(x[n] + offset) + q3*d20 + p*x[n+1] + p3*d2p1;
  1040.   *adxu = d0 + u*d2p1 - u0*d20;
  1041.   if (o > 3) {    /* 5th order */
  1042.     dm2 = x[n-1] - x[n-2];
  1043.     if (dm2 > 180.0)
  1044.       dm2 -= 360.0;
  1045.     if (dm2 < -180.0)
  1046.       dm2 += 360.0;
  1047.     dp2 = x[n+3] - x[n+2];
  1048.     if (dp2 > 180.0)
  1049.       dp2 -= 360.0;
  1050.     if (dp2 < -180.0)
  1051.       dp2 += 360.0;
  1052.     d2m1 = dm1 - dm2;
  1053.     d2p2 = dp2 - dp1;
  1054.     d30  = d20 - d2m1;
  1055.     d3p1 = d2p1 - d20;
  1056.     d3p2 = d2p2 - d2p1;
  1057.     d4p1 = d3p1 - d30;     /* f7 */
  1058.     d4p2 = d3p2 - d3p1;    /* f */
  1059.     *axu  += p5*d4p2 + q5*d4p1;
  1060.     *adxu += u1*d4p2 - u2*d4p1;
  1061.   }
  1062.   return OK;
  1063. }
  1064.  
  1065.  
  1066. /*
  1067. ** position lrz file at proper position for julian date jd;
  1068. ** Return OK or ERR.  Version for outer planets.
  1069. */
  1070.  
  1071. int lrz_file_posit(jd, lrzfpp)
  1072. double jd;     /* full Julian day number, not Astrodienst relative */
  1073. FILE **lrzfpp; /* pointer to file pointer; this function
  1074.                   opens or closes the ephemeris file, and caller
  1075.                   should keep it open while using it. The caller
  1076.                   should close it when he is finished with using
  1077.                   the placalc() package. */
  1078. {
  1079.   int filenr;
  1080.   long posit, jlong;
  1081.   char fname[cchSzDef];
  1082.   static int open_lrznr = -10000; /* local memory to remember whether
  1083.     an already open file is the one with
  1084.     the correct number for this date */
  1085.  
  1086.   jlong = (long)RFloor(jd);
  1087.   filenr = (int)(jlong / EPHE_DAYS_PER_FILE);
  1088.   if (jlong < 0 && filenr * EPHE_DAYS_PER_FILE != jlong)
  1089.     filenr--;
  1090.   posit = jlong - filenr * EPHE_DAYS_PER_FILE;
  1091.   posit = (posit / (int)EPHE_STEP) * EPHE_OUTER_BSIZE;
  1092.   if (*lrzfpp == NULL || open_lrznr != filenr) { /* no or wrong open file */
  1093.     open_lrznr = -10000;
  1094.     sprintf(fname, "%s%s%d", EPHE_OUTER, filenr < 0 ? "M" : "", abs(filenr));
  1095.     if (*lrzfpp != NULL)
  1096.       fclose(*lrzfpp);
  1097.     *lrzfpp = FileOpen(fname, 2);
  1098.     if (*lrzfpp == NULL) {
  1099.       ErrorEphem(fname, -1);
  1100.       return ERR;
  1101.     }
  1102.     open_lrznr = filenr;
  1103.   }
  1104.   if (fseek(*lrzfpp, posit, 0) == 0)
  1105.     return OK;
  1106.   ErrorEphem(fname, posit);
  1107.   return ERR; /* this fseek error occurs only with incomplete files */
  1108. }
  1109.  
  1110.  
  1111. /*
  1112. ** position cpjv file at proper position for julian date jd;
  1113. ** Return OK or ERR.  Version for asteroids.
  1114. ** Sister function to lrz_file_posit().
  1115. */
  1116.  
  1117. int ast_file_posit(jd, astfpp)
  1118. double jd;     /* full Julian day number, not Astrodienst relative */
  1119. FILE **astfpp; /* pointer to file pointer; this function
  1120.                   opens or closes the ephemeris file, and caller
  1121.                   should keep it open while using it. */
  1122. {
  1123.   int filenr;
  1124.   long posit, jlong;
  1125.   char fname[cchSzDef];
  1126.   static int open_astnr = -10000; /* local memory to remember whether
  1127.     an already open file is the one with
  1128.     the correct number for this date */
  1129.  
  1130.   jlong = (long)RFloor(jd);
  1131.   filenr = (int)(jlong / EPHE_DAYS_PER_FILE);
  1132.   if (jlong < 0 && filenr * EPHE_DAYS_PER_FILE != jlong)
  1133.     filenr--;
  1134.   posit = jlong - filenr * EPHE_DAYS_PER_FILE;
  1135.   posit = (posit / (int)EPHE_STEP) * EPHE_ASTER_BSIZE;
  1136.   if (*astfpp == NULL || open_astnr != filenr) { /* no or wrong open file */
  1137.     open_astnr = -10000;
  1138.     sprintf(fname, "%s%s%d", EPHE_ASTER, filenr < 0 ? "M" : "", abs(filenr));
  1139.     if (*astfpp != NULL)
  1140.       fclose(*astfpp);
  1141.     *astfpp = FileOpen(fname, 2);
  1142.     if (*astfpp == NULL) {
  1143.       ErrorEphem(fname, -1);
  1144.       return ERR;
  1145.     }
  1146.     open_astnr = filenr;
  1147.   }
  1148.   if (fseek(*astfpp, posit, 0) == 0)
  1149.     return OK;
  1150.   ErrorEphem(fname, posit);
  1151.   return ERR; /* this fseek error occurs only with incomplete files */
  1152. }
  1153.  
  1154.  
  1155. /*
  1156. ** position chiron file at proper position for julian date jd;
  1157. ** Return OK or ERR. Version for Chiron.
  1158. */
  1159.  
  1160. int chi_file_posit(jd, lrzfpp)
  1161. double jd;  /* full Julian day number, not Astrodienst relative */
  1162. FILE **lrzfpp; /* pointer to file pointer; this function
  1163.                   opens or closes the ephemeris file, and caller
  1164.                   should keep it open while using it */
  1165. {
  1166.   int filenr;
  1167.   long posit, jlong;
  1168.   char fname[cchSzDef];
  1169.   static int open_lrznr = -10000; /* local memory to remember whether
  1170.     an already open file is the one with
  1171.     the correct number for this date */
  1172.  
  1173.   jlong = (long)RFloor(jd);
  1174.   filenr = (int)(jlong / EPHE_DAYS_PER_FILE);
  1175.   if (jlong < 0 && filenr * EPHE_DAYS_PER_FILE != jlong)
  1176.     filenr--;
  1177.   posit = jlong - filenr * EPHE_DAYS_PER_FILE;
  1178.   posit = (posit / (int)EPHE_STEP) * EPHE_CHIRON_BSIZE;
  1179.   if (*lrzfpp == NULL || open_lrznr != filenr) { /* no or wrong open file */
  1180.     open_lrznr = -10000;
  1181.     sprintf(fname, "%s%s%d", EPHE_CHIRON, filenr < 0 ? "M" : "", abs(filenr));
  1182.     if (*lrzfpp != NULL)
  1183.       fclose(*lrzfpp);
  1184.     *lrzfpp = FileOpen(fname, 2);
  1185.     if (*lrzfpp == NULL) {
  1186.       ErrorEphem(fname, -1);
  1187.       return ERR;
  1188.     }
  1189.     open_lrznr = filenr;
  1190.   }
  1191.   if (fseek (*lrzfpp, posit, 0) == 0)
  1192.     return OK;
  1193.   ErrorEphem(fname, posit);
  1194.   return ERR; /* this fseek error occurs only with incomplete files */
  1195. }
  1196. #endif /* PLACALC */
  1197.  
  1198. /* placalc.c */
  1199.